package pagination

import "gorm.io/gorm"

func Pagination(page *int, size *int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if *page < 1 {
			*page = 1
		}
		switch {
		case *size > 100:
			*size = 100
		case *size < 1:
			*size = 10
		}
		return db.Limit(*size).Offset((*page * *size) - *size)
	}
}

func Total(db *gorm.DB) int64 {
	var total int64
	db.Limit(-1).Offset(-1).Count(&total)
	return total
}
